﻿using AutoMapper;
using B.S.Filtration.Domain.到货检验;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Read.Api.Application.Command.到货检验.QualityLnspectionCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Read.Api.Application.CommandHander.到货检验.QualityLnspectionHandler
{

    // GetQualityLnspectionHandler：这是一个处理 GetQualityLnspectionCommand 请求的类，继承自 IRequestHandler 接口。
    public class GetQualityLnspectionHandler : IRequestHandler<GetQualityLnspectionCommand, APIResult<APIPaging<QualityLnspection>>>
    {
        // ISqlSugarClient：这是一个用于数据库操作的客户端，提供了对数据库的访问和操作功能。
        private readonly ISqlSugarClient db;
        // IMapper：这是 AutoMapper 库 的核心接口，用于对象映射。
        private readonly IMapper mapper;
        // 构造函数：GetQualityLnspectionHandler，接收两个参数 db 和 mapper。
        public GetQualityLnspectionHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        // Handle方法：处理GetQualityLnspectionCommand请求
        public async Task<APIResult<APIPaging<QualityLnspection>>> Handle(GetQualityLnspectionCommand request, CancellationToken cancellationToken)
        {
            // 创建一个APIResult对象，类型为APIPaging<QualityLnspection>
            APIResult<APIPaging<QualityLnspection>> result = new APIResult<APIPaging<QualityLnspection>>();
            // 使用SqlSugar查询QualityLnspection表中IsDel为false的记录
            var model = await db.Queryable<QualityLnspection>().Where(x => !x.IsDel).ToListAsync();
            //查询条件
            if (!string.IsNullOrEmpty(request.qualityWorkOrder))
            {
                model = model.Where(x => x.qualityWorkOrder.Contains(request.qualityWorkOrder)).ToList();
            }
            if (!string.IsNullOrEmpty(request.qualityWorkOrderNumber))
            {
                model = model.Where(x => x.qualityWorkOrderNumber.Contains(request.qualityWorkOrderNumber)).ToList();
            }
            if (!string.IsNullOrEmpty(request.GenerateItems))
            {
                model = model.Where(x => x.GenerateItems.Contains(request.GenerateItems)).ToList();
            }
            if (!string.IsNullOrEmpty(request.qualityWorkOrderNumber))
            {
                model = model.Where(x => x.qualityWorkOrderNumber.Contains(request.qualityWorkOrderNumber)).ToList();
            }
            if (!string.IsNullOrEmpty(request.QualityPerson))
            {
                model = model.Where(x => x.QualityPerson.Contains(request.QualityPerson)).ToList();
            }
            if (!string.IsNullOrEmpty(request.QualityDepartment))
            {
                model = model.Where(x => x.QualityDepartment.Contains(request.QualityDepartment)).ToList();
            }
            if (!string.IsNullOrEmpty(request.status))
            {
                model = model.Where(x => x.status.Contains(request.status)).ToList();
            }
            //总记录数
            int totalCount = model.Count();
            //总页数
            int PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            //按Qualityid降序排序，并进行分页
            //skip:挑过前面（页吗-1）*每页大小条记录
            //Take:取当前页的Pagesize条记录
            model = model.OrderByDescending(a => a.Qualityid).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            // 创建 APIPaging 对象，包含总记录数、总页数和当前页数据列表。
            APIPaging<QualityLnspection> paging = new APIPaging<QualityLnspection>()
            {
                TotalCount = totalCount,
                TotalPage = PageCount,
                list = model.ToList()
            };
            // 将结果赋值给 result 对象的 data 属性
            result.code = APIResultCode.success;
            // 将 result 对象的 msg 属性设置为 "显示成功"
            result.msg = "显示成功";
            // 将结果赋值给 result 对象的 data 属性
            result.data = paging;
            // 返回 result 对象
            return result;

        }
    }
}
